#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
/* 策略抽象类 */
class Sort      
{
public:
    virtual void sort(std::vector<int> & v) = 0;
};
/* 冒泡 */
class BubbleSort: public Sort
{
public:
    void sort(std::vector<int> & v)
    {
        for(int i = 0; i < v.size() - 1; i++)
        {
            for(int j = 0; j <v.size() - i - 1; j++)
            {
                if(v[j] > v[j + 1])
                    std::swap(v[j] , v[j + 1]);
            }
        }
    }
};
/* 快排 */
class FastSort: public Sort
{
public:
    void sort(std::vector<int> & v)
    {
        std::sort(v.begin(), v.end());
    }
};
/* 选择解决方法类 */
class Handle
{
public:
    void SetStrategy(Sort *s)       //选择策略
    {
        this->s = s;
    }
    void HandleSort(std::vector<int> & v)
    {
        s->sort(v);
    }
private:
    Sort * s;
};

int main()
{
    Sort *s1 = new BubbleSort;
    Sort *s2 = new FastSort;

    std::vector<int> v = {5, 3, 4, 1, 2};
    Handle *h = new Handle;
    h->SetStrategy(s1);
    /* 用方法类解决问题 */
    h->HandleSort(v);       //输出12345
    std::for_each(v.begin(), v.end(), [](int a){std::cout<<a<<" ";});       //遍历输出
    std::cout<<std::endl;
    return 0;
}